home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Precision Software Appli…tions Silver Collection 1
/
Precision Software Applications Silver Collection Volume One (PSM) (1993).iso
/
windows
/
games
/
tricho.exe
/
TRICHO.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-03-16
|
7KB
|
320 lines
/* tricho.c program that draws fractal inside an equilaterial
* triangle as a result of an apparently random process
* as shown on P.B.S. television show NOVA "THE STRANGE
* NEW SCIENCE OF CHAOS".
* Marty Belles 9-2-89.
*/
#include <windows.h>
#include <stdlib.h>
#include "tricho.h"
long FAR PASCAL WndProc (HWND, unsigned, WORD, LONG);
void DrawTriangle (HWND);
void Newplot (short, short, LONG, HWND);
int Genrand();
short nRed, nGreen, nBlue;
short nFlag = 0;
short xClient, yClient, x, y;
char szAppName [] = "tricho";
int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
HANDLE hInstance, hPrevInstance;
LPSTR lpszCmdLine;
int nCmdShow;
{
HWND hWnd;
MSG msg;
WNDCLASS wndclass;
if (!hPrevInstance) {
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = NULL;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
wndclass.lpszMenuName = szAppName;
wndclass.lpszClassName = szAppName;
if (!RegisterClass (&wndclass))
return FALSE;
}
hWnd = CreateWindow (szAppName, /* window class name */
"Triangle Chaos", /* window caption */
WS_OVERLAPPEDWINDOW, /* window style */
CW_USEDEFAULT, /* initial x position */
0, /* initial y position */
CW_USEDEFAULT, /* initial x size */
0, /* initial y size */
NULL, /* parent window handle */
NULL, /* window menu handle */
hInstance, /* program instance handle */
NULL); /* create parameters */
ShowWindow (hWnd, nCmdShow);
UpdateWindow (hWnd);
while (TRUE) {
if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT)
break;
TranslateMessage (&msg);
DispatchMessage (&msg);
}
else
DrawTriangle (hWnd);
}
return msg.wParam;
}
void DrawTriangle (hWnd)
HWND hWnd;
{
int i;
i = Genrand(); /* dice throw */
switch(i) {
case 1:
case 2:
switch (nFlag) {
case 0:
Newplot(xClient / 2, 0, RGB(nRed, nGreen, nBlue), hWnd);
break;
case 1: /* red */
Newplot(xClient / 2, 0, RGB(255, 0, 0), hWnd);
break;
case 2: /* random */
nRed = rand () % 255;
nGreen = rand () % 255;
nBlue = rand () % 255;
NewPlot (xClient / 2, 0, RGB(nRed, nGreen, nBlue), hWnd);
break;
}
break;
case 3:
case 4:
switch (nFlag) {
case 0: /* blackonwhite or white on black */
Newplot(0, yClient, RGB(nRed, nGreen, nBlue), hWnd);
break;
case 1: /* white */
Newplot(0, yClient, RGB(255, 255, 255), hWnd);
break;
case 2: /* random */
nRed = rand () % 255;
nGreen = rand () % 255;
nBlue = rand () % 255;
NewPlot (0, yClient, RGB(nRed, nGreen, nBlue), hWnd);
break;
}
break;
case 5:
case 6:
switch (nFlag) {
case 0: /* blackonwhite or white on black */
Newplot(xClient, yClient, RGB(nRed, nGreen, nBlue), hWnd);
break;
case 1: /* blue */
Newplot(xClient, yClient, RGB(0, 0, 255), hWnd);
break;
case 2: /* random */
nRed = rand () % 255;
nGreen = rand () % 255;
nBlue = rand () % 255;
NewPlot (xClient, yClient, RGB(nRed, nGreen, nBlue), hWnd);
break;
}
break;
}
}
void Newplot(vx, vy, lColor, hWnd)
HWND hWnd;
long lColor;
short vx, vy;
{
int i, newx, newy;
HDC hDC;
hDC = GetDC(hWnd);
/* calculate new x coordinate */
i = abs(vx - x) / 2;
if (vx > x)
newx = i + x;
else
newx = i + vx;
/* calculate new y coordinate */
i = abs(vy - y) / 2;
if (vy > y)
newy = i + y;
else
newy = i + vy;
/* plot new coordinates */
SetPixel (hDC, newx, newy, lColor);
ReleaseDC (hWnd, hDC);
/* update global variables */
x = newx;
y = newy;
}
int Genrand()
{
int i;
i = rand();
if(i <= 5461)
return 1;
if ((i > 5461) && (i <= 10922))
return 2;
if ((i > 10922) && (i <= 16383))
return 3;
if ((i > 16383) && (i <= 21844))
return 4;
if ((i > 21844) && (i <= 27305))
return 5;
else
return 6;
}
BOOL FAR PASCAL AboutDlgProc (hDlg, iMessage, wParam, lParam)
HWND hDlg;
unsigned iMessage;
WORD wParam;
LONG lParam;
{
switch (iMessage) {
case WM_INITDIALOG:
break;
case WM_COMMAND:
switch (wParam) {
case IDOK:
EndDialog (hDlg, 0);
break;
default:
return FALSE;
}
break;
default:
return FALSE;
}
return TRUE;
}
long FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
HWND hWnd;
unsigned iMessage;
WORD wParam;
LONG lParam;
{
HDC hDC;
static FARPROC lpfnAboutDlgProc;
static HWND hInstance;
switch (iMessage) {
case WM_CREATE:
hInstance = ((LPCREATESTRUCT) lParam)->hInstance;
lpfnAboutDlgProc = MakeProcInstance (AboutDlgProc, hInstance);
break;
case WM_SIZE:
xClient = LOWORD (lParam);
yClient = HIWORD (lParam);
/* plot initial vertices of triangle */
hDC = GetDC(hWnd);
SetPixel (hDC, xClient / 2, 0, 0L); /* center top */
SetPixel (hDC, 0, yClient, 0L); /* bottom left */
SetPixel (hDC, xClient, yClient, 0L); /* bottom right */
ReleaseDC (hWnd, hDC);
/* initialize starting point (arbitrary) */
x = xClient;
y = yClient;
break;
case WM_COMMAND:
switch (wParam) {
case IDM_BLACKONWHITE:
nFlag = 0;
nRed = 0;
nGreen = 0;
nBlue = 0;
SetClassWord (hWnd, GCW_HBRBACKGROUND,
GetStockObject (WHITE_BRUSH));
InvalidateRect (hWnd, NULL, TRUE);
break;
case IDM_WHITEONBLACK:
nFlag = 0;
nRed = 255;
nGreen = 255;
nBlue = 255;
SetClassWord (hWnd, GCW_HBRBACKGROUND,
GetStockObject (BLACK_BRUSH));
InvalidateRect (hWnd, NULL, TRUE);
break;
case IDM_REDWHITEBLUE:
nFlag = 1;
SetClassWord (hWnd, GCW_HBRBACKGROUND,
GetStockObject (BLACK_BRUSH));
InvalidateRect (hWnd, NULL, TRUE);
break;
case IDM_RANDOM:
nFlag = 2;
SetClassWord (hWnd, GCW_HBRBACKGROUND,
GetStockObject (BLACK_BRUSH));
InvalidateRect (hWnd, NULL, TRUE);
break;
case IDM_ABOUT:
DialogBox (hInstance, "TrichoAbout", hWnd,
lpfnAboutDlgProc);
break;
default:
break;
}
break;
case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return DefWindowProc (hWnd, iMessage, wParam, lParam);
}
return 0L;
}